Assignemnt Final
Code
///Name: Derrick Andreasen
///Period: 7
///Program name: Final
///File name: Finalprog.java
///Date Finished:1/21/2016
import java.util.Scanner;
import java.util.Random;
public class Finalprog
{
public static void main( String[] args )
{
Scanner kb = new Scanner(System.in);
Random r = new Random();
int number, h=0, t=0; // h = number of heads landed. t = number of tails landed
System.out.println( "" );
System.out.println( "HEADS OR TAILS" );
System.out.println( "" );
System.out.print( "Number of flips: " );
number = kb.nextInt();
while(number < 1)
{
System.out.println( "That number is too small." );
System.out.print( "New Number: " );
number = kb.nextInt();
while(number > 2100000000)
{
System.out.println( "That number is to big." );
System.out.print( "New Number: " );
number = kb.nextInt();
}
}
if(number <= 2100000000 && number >0)
{
h = 1+r.nextInt(number);
t = number - h;
System.out.println( h +" heads "+ t +" tails" );
}
int numberOfHeads = h, flips = number, numberOfTails = t;
double probabilityOfHeads = numberOfHeads / flips;
double probOfHeads = (double)numberOfHeads;
double probabilityOfTailss = numberOfTails / flips;
double probOfTails = (double)numberOfTails;
System.out.println( "The probability of getting a head is " + probOfHeads );
System.out.println( "The probability of getting a head is " + probOfTails );
}
}
Picture of the output